//提供默认的路径
//提供文本本地化二次解析

var _path: string[] = ["GameHall", "Common"];

class ResManger {
  gameHall_res_cache = {};
  login_res_cache = {};
  common_res_cache = {};
  game_res_cache = {};

  curPath: string = "";
  curType = null;

  complete_callback: Function = null;
  progress_callback: Function = null;

  resType = {
    Atlas: cc.SpriteAtlas,
    Font: cc.Font,
    SpriteFrame: cc.SpriteFrame,
    AnimationClip: cc.AnimationClip,
    SkeletonData: sp.SkeletonData,
    AudioClip: cc.AudioClip,
    Prefab: cc.Prefab,
    Texture2D: cc.Texture2D
  };
  resName = {
    Atlas: "SpriteAtlas",
    Font: "Font",
    SpriteFrame: "SpriteFrame",
    AnimationClip: "AnimationClip",
    SkeletonData: "SkeletonData",
    AudioClip: "AudioClip",
    Prefab: "Prefab",
    Texture2D: "Texture2D"
  };

  init(callBack) {
    // this.loadResDirs(_path, 0, function() {
    //   // console.log("_load end");
    //   if (callBack) {
    //     callBack();
    //   }
    // });
  }

  //
  loadResDirs(assets, index, callBack) {
    if (index >= assets.length) {
      if (callBack) {
        callBack();
      }
      return;
    }

    this.loadResDir(
      assets[index].path,
      assets[index].type,
      null,
      function() {
        this.loadResDirs(assets, ++index, callBack);
      }.bind(this)
    );
  }

  //
  releaseResDirs(assets) {
    for (var i = 0; i < assets.length; i++) {
      this.releaseResDir(assets[i].path, assets[i].type);
    }
  }
  //加载对应目录下所有资源
  loadResDir(
    path: string,
    type: any = cc.Prefab,
    progress_callback: Function = null,
    completedCallback: Function = null
  ) {
    this.curPath = path;
    this.curType = type;
    this.progress_callback = progress_callback;
    this.complete_callback = completedCallback;

    cc.loader.loadResDir(path, type, null, this.completedCallback.bind(this));
  }
  //
  releaseResDir(path: string, type: any = cc.Prefab) {
    cc.loader.releaseResDir(path, type);

    var resName = this.getResName(type);
    if (path === "Common") {
      this.common_res_cache[resName] = {};
    } else if (path === "Hall") {
      this.gameHall_res_cache[resName] = {};
    } else if (path === "Login") {
      this.login_res_cache[resName] = {};
    } else {
      var arr = path.split("/");
      var _path = arr.join("_");
      if (this.game_res_cache[_path]) {
        this.game_res_cache[_path][resName] = {};
      }
    }
  }

  //加载对应目录下所有资源
  loadRes(
    path: string,
    type: any = cc.Prefab,
    progress_callback: Function = null,
    completedCallback: Function = null
  ) {
    this.curPath = path;
    this.curType = type || cc.Prefab;
    this.progress_callback = progress_callback;
    this.complete_callback = completedCallback;

    cc.loader.loadResDir(
      path,
      this.curType,
      null,
      this.completedCallback.bind(this)
    );
  }

  releaseRes(path: string, type: any = cc.Prefab) {
    cc.loader.releaseRes(path, type);
  }

  //   loadResDir(path: string, progress_callback: Function = null,completedCallback: Function = null){
  //       var _resTypes = this.getTypes();
  //       var load_index = 0;
  //       var onLoadingEnd = function () {
  //         if(_resTypes[load_index]) {
  //             this.loadResDirOfType(path, _resTypes[load_index], progress_callback, onLoadingEnd);
  //             load_index = load_index + 1;
  //         } else {
  //             if(completedCallback) {
  //                 completedCallback()
  //             }
  //         }
  //     }.bind(this)
  //     onLoadingEnd();
  //   }
  //   releaseResDir(path: string){
  //     cc.loader.releaseResDir(path);
  //     // for(var i in this.resType){
  //     //     this.releaseResDirOfType(path, this.resType[i])
  //     // }
  //   }

  // 加载结束回调
  completedCallback(err, assets: []) {
    console.log("__", this.curPath);
    for (var i = 0; i < assets.length; i++) {
      this.pushAssets(assets[i]);
    }

    var callback = this.complete_callback;
    if (callback) {
      //加个临时变量，防止回调时已经把call back清空了
      this.complete_callback = null;
      this.progress_callback = null;
      callback(err, assets);
    }
    //     if(!err){
    //         cc.loader.setAutoReleaseRecursively(assets, true);
    //    }
  }

  pushAssets(asset) {
    var resName = this.getResName(this.curType);
    if (this.curPath === "Common") {
      if (!this.common_res_cache[resName]) {
        this.common_res_cache[resName] = {};
      }
      this.common_res_cache[resName][asset.name] = asset;
    } else if (this.curPath === "Hall") {
      if (!this.gameHall_res_cache[resName]) {
        this.gameHall_res_cache[resName] = {};
      }
      this.gameHall_res_cache[resName][asset.name] = asset;
    } else if (this.curPath === "Login") {
      if (!this.login_res_cache[resName]) {
        this.login_res_cache[resName] = {};
      }
      this.login_res_cache[resName][asset.name] = asset;
    } else {
      var arr = this.curPath.split("/");
      var _path = arr.join("_");
      if (!this.game_res_cache[_path]) {
        this.game_res_cache[_path] = {};
        this.game_res_cache[_path][resName] = {};
      } else if (!this.game_res_cache[_path][resName]) {
        this.game_res_cache[_path][resName] = {};
      }
      this.game_res_cache[_path][resName][asset.name] = asset;
    }
  }

  getRes(name, type: any = cc.Prefab) {
    var resName = this.getResName(type);
    var res = null;
    for (var i in this.game_res_cache) {
      if (
        this.game_res_cache[i][resName] &&
        this.game_res_cache[i][resName][name]
      ) {
        res = this.game_res_cache[i][resName][name];
        break;
      }
    }
    if (!res) {
      res = (this.gameHall_res_cache[resName] || {})[name] || null;
    }
    if (!res) {
      res = (this.login_res_cache[resName] || {})[name] || null;
    }

    if (!res) {
      res = (this.common_res_cache[resName] || {})[name] || null;
    }

    return res;
  }

  findKey(obj, value, compare = (a, b) => a === b) {
    return Object.keys(obj).find(k => compare(obj[k], value));
  }
  getResName(resType) {
    var key = this.findKey(this.resType, resType);
    var name = this.resName[key];
    return name;
  }
  getTypes() {
    var _resTypes = [];
    var j = 0;
    for (var i in this.resType) {
      _resTypes[j++] = this.resType[i];
    }
    return _resTypes;
  }
}

export let ResMgr: ResManger = new ResManger();
